//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////
declare const $
class LoadingUI extends egret.Sprite implements RES.PromiseTaskReporter {
    private animationSpeed = 800
    private show_loading_text = 1
    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
    }

    private textField: egret.TextField;

    private init() {
        $('#mask').hide()
        // this.getUserInfo()
        this.createView();
        this.createText();
        this.createHeart();
        this.createProgress();
        this.createLoadingTip();

        // this.addChild(new Music())
    }

    private createView(): void {
        this.textField = new egret.TextField();
        // this.addChild(this.textField);
        this.textField.width = this.stage.stageWidth;
        this.textField.height = 100;
        this.textField.textAlign = "center";
        this.textField.y = this.stage.stageHeight / 2;
    }

    public onProgress(current: number, total: number): void {
        const num = (current / total * 100).toString();
        const miva = parseInt(num);
        this.textField.text = `${miva} %`;

        this.progressContainer.graphics.beginFill(0xffffff, 1)
        this.progressContainer.graphics.drawRect(0, 0, parseInt(`${this.progressContainer.width * (miva / 100)}`), 7)
        this.progressContainer.graphics.endFill()
    }

    public loadingComplete() {
        console.log('加载完成');
        this.show_loading_text++
        // this.dispatchEvent(new egret.Event('loadingComplete'))
    }

    private createText() {
        const contaienr = new egret.Sprite()
        this.addChild(contaienr)
        contaienr.width = this.stage.stageWidth
        for (let i = 1; i <= 7; i++) {
            const text = Utils.createBitmapByName(`item_loading_text_${i}_png`)
            Utils.createScale(text, contaienr)
            text.anchorOffsetX = text.width / 2
            text.x = this.stage.stageWidth / 2
            text.y = 100 * i - 50
            text.alpha = 0
            egret.Tween.get(text)
                .wait(this.animationSpeed * (i - 1))
                .to({ y: text.y + 50, alpha: 1 }, this.animationSpeed)
        }

        this.addEventListener('click_loading_heart', () => {
            egret.Tween.get(contaienr).to({ alpha: 0 }, this.animationSpeed)
        }, this)
    }

    private createHeart() {
        const heart = Utils.createBitmapByName('item_heart_png')
        Utils.createScale(heart, this, .15)
        Utils.anchorCenter(heart)
        heart.x = this.stage.stageWidth / 2
        heart.y = 900
        heart.alpha = 0
        const { scaleX: OSX, scaleY: OSY } = heart
        egret.Tween.get(heart)
            .wait(this.animationSpeed * 7)
            .to({ alpha: 1 }, this.animationSpeed)
            .call(() => {
                egret.Tween.get(heart, { loop: true })
                    .to({ scaleX: heart.scaleX - .02, scaleY: heart.scaleY - .02 }, 60000 / 144)
                    .to({ scaleX: heart.scaleX, scaleY: heart.scaleY }, 60000 / 144)
                    .wait(60000 / 144)
                this.show_loading_text++
                heart.touchEnabled = true
                heart.once(egret.TouchEvent.TOUCH_TAP, () => {
                    if (this.show_loading_text < 3) {
                        return console.log('wait')
                    }
                    console.log('播放bgm')
                    const bgm = RES.getRes('bgm_mp3')
                    bgm.play()
                    this.dispatchEvent(new egret.Event('click_loading_heart'))
                    egret.Tween.removeTweens(heart)
                    egret.Tween.get(heart)
                        .to({ y: this.stage.stageHeight / 2, scaleX: OSX, scaleY: OSY }, this.animationSpeed)
                        .to({ y: this.stage.stageHeight / 2 }, this.animationSpeed)
                        .wait(this.animationSpeed)
                        .call(() => {
                            this.dispatchEvent(new egret.Event('loadingComplete'))
                            this.stage.dispatchEvent(new egret.Event('play_bgm'))
                        })
                }, this)
            })
    }

    private createLoadingTip() {
        const text = Utils.createText('点击爱心进入', { x: 0, y: 1080 }, this)
        text.stroke = 0
        text.size = 20
        text.bold = false
        text.textColor = 0xffffff
        text.width = this.stage.stageWidth
        text.textAlign = egret.HorizontalAlign.CENTER
        text.alpha = 0
        text.addEventListener(egret.Event.ENTER_FRAME, showHandler, this)
        function showHandler() {
            console.log(this.show_loading_text)
            if (this.show_loading_text >= 3) {
                ParamsPool.LOADING_GOTO = 'gotoCheckMap'
                ParamsPool.LOADING_TIP = '正在进入地区选择'
                egret.Tween.get(text).to({ alpha: 1 }, this.animationSpeed)
                text.removeEventListener(egret.Event.ENTER_FRAME, showHandler, this)
            }
        }

        this.addEventListener('click_loading_heart', () => {
            egret.Tween.get(text).to({ alpha: 0 }, this.animationSpeed)
        }, this)
    }

    private progressContainer
    private createProgress() {
        const contaienr = new egret.Sprite()
        this.progressContainer = contaienr
        this.addChild(contaienr)
        contaienr.graphics.lineStyle(1, 0xffffff)
        contaienr.graphics.beginFill(0, 0)
        contaienr.graphics.drawRect(0, 0, this.stage.stageWidth * .6, 7)
        contaienr.graphics.endFill()

        Utils.anchorCenter(contaienr)
        contaienr.x = this.stage.stageWidth / 2
        contaienr.y = 1020

        this.addEventListener('click_loading_heart', () => {
            egret.Tween.get(contaienr).to({ alpha: 0 }, this.animationSpeed)
        }, this)
    }

    // private getUserInfo() {
    //     if (!$('#avatar').html()) {
    //         window.location.href = "http://www.unmcc.com/unmcc/miva/LZ_OLG" + "?" + Math.random();
    //         return;
    //     }
    //     ParamsPool.USER_AVATAR = $('#avatar').html()
    //     console.log($('#avatar').html())
    //     $.get('http://39.104.181.160:7489/H5').then(res => {
    //         console.log(res)
    //         this.show_loading_text++
    //         ParamsPool.PLAYER_NUM = res.data.count
    //         ParamsPool.PLAYER_LIST = res.data.rows
    //     })
    // }

}
